home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / doc / homepage / tutorials / lesson7 / shareeg2 / moveball.java < prev    next >
Encoding:
Java Source  |  1996-08-22  |  2.8 KB  |  97 lines

  1. /*
  2.   This Java file is an example of synchronized multi-user
  3.   behaviour. The behaviour is of a ball moving in a loop.
  4.   In all client worlds the ball is synchronized.
  5.  
  6.   Written By : Jai Natarajan
  7.                Sony Pictures Imageworks
  8. */
  9.  
  10. import vrml.*;
  11. import vs.*;
  12. import java.lang.*;
  13. import java.util.*;
  14. import java.io.*;
  15.  
  16. public class trial extends Script {
  17.     float [] currPos = {0,0,0};
  18.  
  19.     SFNode obj1 = (SFNode)getField("obj1");
  20.     Transform objT = (Transform)obj1.getValue();
  21.     SFVec3f obj1position = (SFVec3f)getEventOut("obj1position");
  22.  
  23.         SFBool timerOn = (SFBool)getEventOut("timerEnable");    
  24.  
  25.     public void trial () { }
  26.  
  27.         // move : called by the timer every tick
  28.         // ignored by everyone except the master, who updates
  29.     // position and sends it to the others
  30.         public void move(ConstSFTime ev, ConstSFTime time) {
  31.       int j;
  32.  
  33.          if(Vscp.amIMaster()) {
  34.            for(j = 0; j < 3; j++) {
  35.            currPos[j] += 0.2;
  36.            currPos[j] = currPos[j] % 5;
  37.         } 
  38.                 String s = createStringFromPos(currPos);    
  39.             Vscp.sendApplSpecificMsgWithDist(obj1, "rpc_place_ball", 
  40.                          s,Vscp.allClientsExceptMe);
  41.             place_ball();
  42.              }
  43.         }
  44.  
  45.         // createStringFromPos : takes a position triplet and encodes
  46.     // as a String to be passed as parameters for the rpc call
  47.         public String createStringFromPos(float [] pos) {
  48.         Float f0 = new Float(pos[0]);
  49.         Float f1 = new Float(pos[1]);
  50.         Float f2 = new Float(pos[2]);
  51.             String delim = new String(" ");    
  52.         String s00 = f0.toString();
  53.         String s0 = s00.concat(delim);
  54.         String s11 = f1.toString();
  55.         String s1 = s11.concat(delim);
  56.         String s22 = f2.toString();
  57.         String s2 = s22.concat(delim);
  58.  
  59.                 String s01 = s0.concat(s1);
  60.         String ans = s01.concat(s2);
  61.         return(ans); 
  62.         }
  63.  
  64.         // getPosFromString : retrieves position triplet from String
  65.     public float [] getPosFromString(String s) {
  66.         float [] ans = new float[3];
  67.             String delim = new String(" ");    
  68.         StringTokenizer st = new StringTokenizer(s, delim);
  69.  
  70.         String s0 = st.nextToken();
  71.         String s1 = st.nextToken();
  72.         String s2 = st.nextToken();
  73.  
  74.                 Float f0 = new Float (s0);        
  75.                 Float f1 = new Float (s1);        
  76.                 Float f2 = new Float (s2);        
  77.  
  78.                 ans[0] = f0.floatValue();
  79.                 ans[1] = f1.floatValue();
  80.                 ans[2] = f2.floatValue();
  81.        
  82.         return(ans);
  83.         }
  84.  
  85.         // place_ball : Used by Master to put the ball in its new position
  86.     public void place_ball () {
  87.         obj1position.setValue(currPos);
  88.         }
  89.  
  90.         // rpc_place_ball : called in clients (non-master) to positon ball
  91.     public void rpc_place_ball (ConstSFString data, ConstSFTime time) {
  92.         currPos = getPosFromString(data.getValue());    
  93.         place_ball();
  94.         }
  95. }
  96.     
  97.